package com.yuanmei.charts;

import android.content.Context;
import android.graphics.Paint;
import android.text.Html;
import android.text.Spanned;

import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.CandleData;
import com.github.mikephil.charting.data.CandleDataSet;
import com.github.mikephil.charting.data.CandleEntry;
import com.github.mikephil.charting.data.CombinedData;
import com.yuanmei.model.KLineChartQuote;
import com.yuanmei.views.R;

import java.util.ArrayList;
import java.util.List;

public class MarketIndexChartDataSet implements TaChartData {
    private Context mContext;
    private List<CandleEntry> mCandleList;
    private List<String> mXAxisDates;
    private CombinedData mKLineCombineData;

    private String mIndexName;

    public MarketIndexChartDataSet(Context context, List<KLineChartQuote> indexDayQuotes, String miName) {
        mContext = context;
        mIndexName = miName;
        mXAxisDates = new ArrayList<>();
        mCandleList = new ArrayList<>();
        List<BarEntry> bgBarList = new ArrayList<>();
        for (int i = 0; i < indexDayQuotes.size(); i++) {
            KLineChartQuote quote = indexDayQuotes.get(i);
            mXAxisDates.add(quote.getQuoteDate());
            float close = quote.getQuoteClose();
            float open = quote.getQuoteOpen();
            float high = quote.getQuoteHigh();
            float low = quote.getQuoteLow();
            mCandleList.add(new CandleEntry(i, high, low, open, close));
            bgBarList.add(new BarEntry(low, open));
        }

        CandleDataSet candleSet = new CandleDataSet(mCandleList, "");
        candleSet.setDrawHorizontalHighlightIndicator(false);
        candleSet.setHighlightEnabled(true);
        candleSet.setAxisDependency(YAxis.AxisDependency.LEFT);
        candleSet.setShadowWidth(1f);
        candleSet.setValueTextSize(10f);
        candleSet.setDecreasingColor(mContext.getResources().getColor(R.color.decreasing_color));
        candleSet.setDecreasingPaintStyle(Paint.Style.FILL);
        candleSet.setIncreasingColor(mContext.getResources().getColor(R.color.increasing_color));
        candleSet.setIncreasingPaintStyle(Paint.Style.STROKE);
        //设置开盘价等于收盘价的颜色
        candleSet.setNeutralColor(mContext.getResources().getColor(R.color.decreasing_color));
        candleSet.setShadowColorSameAsCandle(true);
        candleSet.setHighlightLineWidth(1f);
        candleSet.setHighLightColor(mContext.getResources().getColor(R.color.border_color));
        candleSet.setDrawValues(false);
        candleSet.setValueTextColor(mContext.getResources().getColor(R.color.marker_text_color));

        BarDataSet barSet = new BarDataSet(bgBarList, "");
        barSet.setDrawValues(false);
//        barSet.setBarShadowColor(R.color.white);
        barSet.setBarShadowColor(mContext.getResources().getColor(R.color.transparent));
        barSet.setColors(mContext.getResources().getColor(R.color.transparent));
        BarData barData = new BarData(barSet);

        mKLineCombineData = new CombinedData();
        mKLineCombineData.setData(barData);
        mKLineCombineData.setData(new CandleData(candleSet));
    }

    @Override
    public int getXAxisSize() {
        return mXAxisDates.size();
    }

    @Override
    public Spanned getDescription(int index) {
//        String open = "";
//        if (index >= 0 && index < mCandleList.size()) {
//            open = Utils.priceToDecimalString(mCandleList.get(index).getOpen());
//        }
//        String high = "";
//        if (index >= 0 && index < mCandleList.size()) {
//            high = Utils.priceToDecimalString(mCandleList.get(index).getHigh());
//        }
//        String low = "";
//        if (index >= 0 && index < mCandleList.size()) {
//            low = Utils.priceToDecimalString(mCandleList.get(index).getLow());
//        }
//        String close = "";
//        if (index >= 0 && index < mCandleList.size()) {
//            close = Utils.priceToDecimalString(mCandleList.get(index).getClose());
//        }
//        String str1 = String.format(
//                "%s: o:%s, h:%s, l:%s, c:%s",
//                mIndexName, open, high, low, close);
        return  Html.fromHtml(mIndexName);
    }

    @Override
    public String getXAxisLabel(int index) {
        String date = "None";
        if (index >= 0 && index < mXAxisDates.size()) {
            date = mXAxisDates.get(index);
        }
        return date;
    }

    @Override
    public List<String> getXAxisLabelList() {
        return mXAxisDates;
    }

    @Override
    public CombinedData getTaChartData() {
        return mKLineCombineData;
    }
}
